#include <stdio.h>
#include <unistd.h>
#include <termios.h>

#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>

value readchar(value unit) {
  struct termios oldt, newt;
  char ch[2];

  CAMLparam1(unit);

  ch[1]='\0';

  tcgetattr( STDIN_FILENO, &oldt );
  newt = oldt;
  newt.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
  ch[0] = getchar();
  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );

  CAMLreturn (caml_copy_string(ch));
}
